<template>
	<div class="allcontainer">
		<div class="h2">作业列表</div>
		<div class="bg-white">
			<!-- <div id="components-form-demo-advanced-search" >
				<a-form class="ant-advanced-search-form home-form" :form="form">
					<a-row :gutter="24">
						<a-col :span="8">
							<a-form-item label="关键字">
								<a-input autocomplete="off" class="field-right" placeholder="请输入关键字搜索"
									v-decorator="[`key`]" />
							</a-form-item>
						</a-col>
					
					</a-row>
				</a-form>
			</div> -->
			<a-button-group size="default" class="option home-form" v-if="$route.query.State!==5">
				<!-- <a-button type="primary" @click="handleSearch">搜索</a-button>
			<a-button @click="handleReset">重置</a-button> -->
				<a-button type="primary" v-if="($route.query.source==1&&Number($route.query.State)<4)|| ($route.query.source==3&&$route.query.State==3)" @click="addList">添加作业</a-button>
				<!-- <a-button type="primary" @click="exportList()">导出</a-button> -->
				<!-- <a-button type="danger" class="btn" @click="deleteAllList">批量删除</a-button> -->
			</a-button-group>
			<div class="table">
				<!-- <el-table stripe :data="data" v-loading="loading" border :max-height="tableHeight"
				@selection-change="handleSelectionChange" highlight-current-row style="width: 100%;"> -->
				<el-table stripe ref="singleTable" :data="data" highlight-current-row style="width: 100%">
					<!-- <el-table-column type="selection" width="55" fixed="left"></el-table-column> -->
					<el-table-column type="index" label="序号" width="60" fixed="left" align="center"></el-table-column>
					<el-table-column prop="Title" label="题目名称" min-width="150"></el-table-column>

					<el-table-column prop="Name" label="答题方式" min-width="240" align="center">
						<template slot-scope="scope">
							<div style="text-align: center;">
								
								{{ scope.row.Type=='1'?'多行文本在线输入':scope.row.Type=='2'?'上传附件':'' }}
							</div>

						</template>
					</el-table-column>
					
					<el-table-column label="操作" width="200" fixed="right">
						<template slot-scope="scope">
							<div class="user-btn" style="text-align:center">
								<!-- source  1是发起的活动  2是参加的活动 3是管理的活动入口 -->
								<span class="modify" v-if="$route.query.source!==2&&$route.query.State==3" @click="modifyList(scope.row, 'modify')">修改</span>
								<span class="adopt" v-if="$route.query.source==2&&$route.query.State!==4&&$route.query.State!==5" @click="makework(scope.row)">做作业</span>
								<span class="delete" v-if="$route.query.source!==2&&$route.query.State==3" @click="deleteList(scope.row)">删除</span>
							<!-- 	<span class="shenpi" v-if="$route.query.source==3" @click="makeSure(scope.row)">去确认</span> -->
							</div>
						</template>
					</el-table-column>
				</el-table>
				<el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
					:current-page="pagination.currentPage" :page-sizes="pagination.pageSizeOptions"
					:page-size="pagination.pageSize" layout="total, sizes, prev, pager, next, jumper"
					:total="pagination.total"></el-pagination>
			</div>
		</div>
		<add :ActivityTypePrizeTypeList="ActivityTypePrizeTypeList" :ActivityMemberTypeList="ActivityMemberTypeList"
			:ActivityTypeData="ActivityTypeData" :text="text" :DepartCodeList="DepartCodeList" :TypeIDList="TypeIDList"
			v-show="addIf" @closeFun="closeFun">
		</add>
		<addItem v-show="addIfItem" :text="textItem" @closeFun="closeFun"> </addItem>
		<!-- <clubDetail :text="text1" v-show="clubDetailShow" @closeFun="closeFun"></clubDetail> -->
	</div>
</template>


<script>
	function getBase64(img, callback) {
		const reader = new FileReader();
		reader.addEventListener('load', () => callback(reader.result));
		reader.readAsDataURL(img);
	}
	import add from './add';
	import addItem from './addItem';
	import ClubSearch from '@/components/SearchForm/ClubSearch.vue'
	// import clubDetail from './clubDetail';
	import Func from '@/utils/func';
	import Cache from '@/utils/cache';
	import {
		UserActivityJobDoGetPageList,
		UserActivityJobDoDelete,
		GetUserList,
		EnableClub,
		GetClubTypeList,
		GetDepartList,
		UserDoUpdateBase64Image,
		UserDoSetHeadImage,
		GetActivityTypeList,
		CommonDoGetActivityTypeScoreList,
		CommonDoGetActivityTypePrizeTypeList,
		DoGetActivityMemberTypeListCommon,
		DoGetSemesterListCommon,
		ManageCreditSessionDoGetPageList,
		UserCreditApplyDoSubmit,
		UserCreditApplyDoCancel
	} from '@/api/follow';
	const data = [];
	export default {
		components: {
			add,
			addItem,
			ClubSearch
		},
		data() {
			return {
				textItem:{},
				addIfItem:false,
				CreditSessionData: [],
				SemesterList: [],
				stateData: [{
					ID: '1',
					Name: "状态值未给"
				}, ],
				applyTypeData: [{
						ID: '1',
						Name: "参与申请"
					},
					{
						ID: '2',
						Name: "获奖申请"
					}
				],
				ActivityTypePrizeTypeList: [],
				ActivityMemberTypeList: [],
				ActivityTypeData: [],
				data,
				form: this.$form.createForm(this, {
					name: 'advanced_search'
				}),
				pagination: {
					currentPage: 1,
					pageSize: this.global.pageSize,
					pageSizeOptions: this.global.pageSizeOptions, // 每页中显示的数据
					showTotal: total => `共有 ${total} 条数据` // 分页中显示总的数据
				},
				addIf: false,
				text: {},
				visible: false,
				confirmLoading: false,
				itemId: '',
				departmentList: [],
				worklist: [],
				loading: false,
				values: {},
				pages: {
					pageIndex: 2,
					pageSize: 20
				},
				tableHeight: parseFloat(window.innerHeight - 160),
				TypeIDList: [],
				DepartCodeList: [],
				fileList: [],
				clubDetailShow: false,
				text1: {},
				requestUrl: this.global.requestUrl,
				token: Cache.get('token'),
				teacherList: [],
				memberList: [],
				multipleSelection: []
			};
		},
		computed: {},
		created() {
			this.getList();
			//this.getOtherList();
		},
		mounted() {},
		methods: {
			// makeSure(text){
				
			// },
			makework(text){
				console.log(text)
				this.textItem = {
					...text,
					time: new Date()
				};
				this.addIfItem=true
			},
			searchValueFun(name, value) {
				this.values = {
					...this.values,
					[name]: value
				}
			},
			filterOption(input, option) {
				return option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0;
			},
			handleSelectionChange(val) {
				this.multipleSelection = val;
			},
			chehui(item) {
				let that = this;
				this.$confirm({
					title: '提示',
					content: '确定撤回申请吗？',
					okText: '确认',
					cancelText: '取消',
					async onOk() {
						let data = {}
						data.id = item.ID
						let res = await UserCreditApplyDoCancel(data);
						that.getList();
					}
				});
			},
			tijao(item) {
				let that = this;
				this.$confirm({
					title: '提示',
					content: '确定提交申请吗？',
					okText: '确认',
					cancelText: '取消',
					async onOk() {
						let data = {}
						data.id = item.ID
						let res = await UserCreditApplyDoSubmit(data);
						that.getList();
					}
				});
			},
			deleteAllList() {
				if (this.multipleSelection.length == 0) {
					this.$message.error('请先选择要删除的数据！');
				} else {
					let that = this;
					this.$confirm({
						title: '提示',
						content: '确定要删除选中的数据吗？',
						okText: '确认',
						cancelText: '取消',
						async onOk() {
							let list = that.multipleSelection;
							for (let i = 0, len = list.length; i < len; i++) {
								let data = {};
								data.id = list[i].ID;
								let res = await UserActivityJobDoDelete(data);
							}
							that.getList();
						}
					});
				}
			},
			async fetchteacher(value) {
				// let myReg = /^[^a-zA-Z]+$/;
				if (value && value.length > 1) {
					let data = {};
					data.key = value;
					data.top = 20;
					let res = await GetUserList(data);
					this.teacherList = res.data.data;
				}
			},
			async fetchmember(value) {
				// let myReg = /^[^a-zA-Z]+$/;
				if (value && value.length > 1) {
					let data = {};
					data.key = value;
					data.top = 20;
					let res = await GetUserList(data);
					this.memberList = res.data.data;
				}
			},
			exportList() {
				this.form.validateFields((error, values) => {
					// ticket
					let hrefUrl = this.requestUrl + 'Manage/CreditApply/DoExport?userToken=' + encodeURIComponent(
						this
						.token);
					let hrefStr = '';
					for (let key in values) {
						if (values[key]) {
							hrefStr += key + '=' + values[key] + '&';
						}
					}
					window.open(hrefUrl + '&' + hrefStr, '_blank');
				});
			},
			beforeUpload(file) {
				this.fileList = [...this.fileList, file];
			},
			async handleChange(info) {
				if (info.file.originFileObj) {
					getBase64(info.file.originFileObj, imageUrl => {
						this.imageUrl = imageUrl;
						let data = {};
						data.data = this.imageUrl.substr(22);
						Promise.resolve(UserDoUpdateBase64Image(data)).then(v => {
							let data1 = {};
							data1.headImage = v.data.url;
							Promise.resolve(UserDoSetHeadImage(data1)).then(m => {});
						});
					});
				} else {
					return false;
				}
			},
			handleSearch(e) {
				e.preventDefault();
				this.form.validateFields((error, values) => {
					this.pagination.currentPage = 1;
					if (values.data && values.data.length > 0) {
						values.startTime = values.data[0].format('YYYY-MM-DD')
						values.endTime = values.data[1].format('YYYY-MM-DD')
					}
					this.values = values;
					this.getList();
				});
			},
			handleReset() {
				this.form.resetFields();
				this.values = {};
				this.getList();
			},
			handleSizeChange(val) {
				this.pagination.pageSize = val;
				this.getList();
			},
			handleCurrentChange(val) {
				this.pagination.currentPage = val;
				this.getList();
			},
			async getList() {
				this.loading = true;
				let data = {};
				if (this.values) {
					data = this.values;
				}
				data.activityID=this.$route.query.ID;
				data.pageIndex = this.pagination.currentPage;
				data.pageSize = this.pagination.pageSize;
				let res = await UserActivityJobDoGetPageList(data);
				this.loading = false;
				this.data = res.data.data;
				const pagination = {
					...this.pagination
				};
				pagination.total = res.data.pageInfo.TotalItemCount;
				this.pagination = pagination;
			},
			async getOtherList() {
				let res = await GetClubTypeList();
				let res2 = await GetDepartList();
				this.TypeIDList = res.data.data;
				this.DepartCodeList = res2.data.data;
				let res3 = await GetActivityTypeList()
				this.ActivityTypeData = res3.data.data
				let res4 = await CommonDoGetActivityTypePrizeTypeList()
				this.ActivityTypePrizeTypeList = res4.data.data

				let res5 = await DoGetActivityMemberTypeListCommon();
				this.ActivityMemberTypeList = res5.data.data;
				let res6 = await DoGetSemesterListCommon();
				this.SemesterList = res6.data.data;

				let res7 = await ManageCreditSessionDoGetPageList(data);
				this.CreditSessionData = res7.data.data;

			},
			addList() {
				this.text = {};
				this.addIf = !this.addIf;
			},
			modifyList(text) {
				this.text = {
					...text,
					time: new Date()
				};
				this.addIf = !this.addIf;
			},
			enableList(text) {
				let that = this;
				this.$confirm({
					title: '提示',
					content: `您确定要${text.IsEnable ? '停用' : '启用'}该学生组织吗？`,
					okText: '确认',
					cancelText: '取消',
					async onOk() {
						let data = {};
						data.id = text.ID;
						let res = await EnableClub(data);
						if (res.data.code == 0) {
							that.getList();
							that.$message.success(res.data.msg);
						} else {
							that.$message.error(res.data.msg);
						}
					}
				});
			},
			deleteList(text) {
				let that = this;
				this.$confirm({
					title: '提示',
					content: `您确定删除该数据吗？`,
					okText: '确认',
					cancelText: '取消',
					async onOk() {
						let data = {};
						data.id = text.ID;
						let res = await UserActivityJobDoDelete(data);
						if (res.data.code == 0) {
							that.getList();
							that.$message.success(res.data.msg);
						} else {
							that.$message.error(res.data.msg);
						}
					}
				});
			},
			seeList(text, name) {
				this.$router.push({
					name: name,
					query: {
						ID: text.ID,
						Name: text.Name
					}
				});
			},
			seeDetail(item) {
				// this.text1 = {
				// 	...item,
				// 	time: new Date()
				// };
				// this.clubDetailShow = true;
				// this.text = {
				// 	...text,
				// 	time: new Date(),
				// 	EditType:3
				// };
				// this.addIf = !this.addIf;
			},
			closeFun(data) {
				this.addIf = false;
				this.addIfItem=false;
				this.clubDetailShow = false;
				if (data == 1) {
					this.getList();
				}
			}
		}
	};
</script>
